// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    discover: [{
      id: 1,
      text: "IT及其他"
    },
    {
      id: 2,
      text: "消防"
    },
    {
      id: 3,
      text: "数据中心/机房"
    },
    {
      id: 4,
      text: "音视频"
    },
    {
      id: 5,
      text: "设备管理"
    },
    {
      id: 6,
      text: "信息设施"
    },
    {
      id: 7,
      text: "公共安全"
    },
    {
      id: 8,
      text: "建筑智能化综合"
    },
    {
      id: 9,
      text: "系统调试"
    },
    {
      id: 10,
      text: "项目造价"
    },
    {
      id: 11,
      text: "项目设计"
    },
    {
      id: 12,
      text: "项目管理"
    },
    {
      id: 13,
      text: "工程安装"
    }
    ],
    searchContent: "",
    searchList: [],
    input: ""
  },
  // 获取搜索框的内容
  searchContent: function (e) {
    this.setData({
      searchContent: e.detail.value
    }),
      console.log(this.data.searchContent)
  },
  // // 失去焦点清除input框内容
  onBindInputBlur: function (event) {
    e.detail.value = ""
  },
  // 搜索按钮
  search: function (e) {

    if (this.data.searchContent == "") {
      wx.showToast({
        title: '请输入搜索内容',  
        icon: 'none',
        duration: 1500,
        mask: false,
        success: (result) => {

        },
        fail: () => { },
        complete: () => { }
      });

    } else {
      var obj = this.data.searchContent;
      // wx.setStorageSync("receiveHistory", obj);
      // console.log(obj);
      // var history = wx.getStorageSync("receiveStore");
      var that = this;
      // if (wx.getStorageSync("receiveStore") == "") {
      //   console.log("222");
        // that.setData({
        //   searchList: history
        // })
     
      // else {

      //   that.data.searchList = history;
      //   that.setData({
      //     searchList: that.data.searchList
      //   });
      // }


      console.log(that.data.searchList);
      //  名称相同的移除
      for (var i = 0; i < that.data.searchList.length; i++) {
          for(var j=i+1;j<that.data.searchList.length;j++){
            if(that.data.searchList[i]==that.data.searchList[j]){
              that.data.searchList.splice(j,1);
              j--;
              that.setData({
                searchList:that.data.searchList
              })
            }
          }
        
      }
      // 超过20位移除一位
      if (this.data.searchList.length > 20) {
        var num = that.data.searchList.pop()
        that.setData({
          searchList: num
        })
        console.log(that.data.searchList)
      };
      console.log(that.data.searchList);
      that.data.searchList.unshift(that.data.searchContent);
      that.setData({
        searchList:that.data.searchList
      })
      wx.setStorageSync("receiveStore", that.data.searchList)
      wx.navigateTo({
        url: '../searchResult/searchResult?name=' + that.data.searchContent,
      })
    }
    
  },
  clear: function () {
    this.setData({
      searchList: []
    })
    wx.setStorageSync("receiveStore", that.data.searchList);
    // wx.clearStorageSync("historyStore")
  },
  // history:function(){
  //   var that = this;
  //   console.log(that.data.searchContent)
  //   wx.navigateTo({
  //     url: '../demand/demand?name=' + that.data.searchContent,
  //   })
  // },
  history: function (e) {
    var that = this;
    var item = e.currentTarget.dataset.item;
    that.setData({
      searchContent: item
    })
    wx.navigateTo({
      url: '../searchResult/searchResult?name=' + that.data.searchContent,
    })
  

  },
  push: function (e) {
    var that = this;
    console.log(that.data.searchContent)
    var that = this;
    var name = e.currentTarget.dataset.name;
    // console.log(text);
    this.setData({
      name: name,
      searchContent:name
    })
    this.search();
   
    // wx.navigateTo({
    //   url: '../searchResult/searchResult?name=' + that.data.searchContent,
    // })

  },
  // 根据搜索来返回不同页面

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: 'https://www.xiaozheng8.com/serviceCate/findSmallCate',
      data: {},
      method: "post",
      success: function (res) {
        console.log(res);
        that.setData({
          discover: res.data.data
        })
        console.log(that.data.discover);
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    if (wx.getStorageSync("receiveStore") == "") {
      return false;
    } else {
      var num = wx.getStorageSync("receiveStore");
      this.setData({
        searchList: num
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})